package com.ysactive.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import com.ysactive.config.EmailConfiguration;
import lombok.Cleanup;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Component;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Notice:
 *
 * @author xuxu
 * @version 1.0
 * @date 2023/9/2
 * @since 1.0
 */

@Slf4j
@Setter
@RequiredArgsConstructor
@Component
public class FileService {

    private final EmailConfiguration emailConfiguration;

    public void sendRegCode(String email, String code){
        //根据code发送邮件

        // 没有抽取到配置文件中，后期可以提取到配置中
        String content = "你好,验证码是：<h2> {code} </h2>";
        Map<String, String> params = new HashMap<>();
        params.put("code", code);
        sendHtmlEmail(email, "注册", content, params);

    }


    /**
     * 抛出异常慢点为发送失败
     * @param email
     * @param title
     * @param content
     * @param params
     */
    @SneakyThrows
    public void sendHtmlEmail(String email, String title, String content, Map<String, String> params){

        PropertyPlaceholderHelper pph = new PropertyPlaceholderHelper("{", "}");

        Properties p = new Properties();
        p.putAll(params);

        String s = pph.replacePlaceholders(content, p);

        MailAccount account = new MailAccount();
        account.setHost(emailConfiguration.getHost());
        account.setPort(emailConfiguration.getPort());
        account.setFrom(emailConfiguration.getForm());
        account.setUser(emailConfiguration.getUser());
        account.setPass(emailConfiguration.getPassword());
        account.setStarttlsEnable(emailConfiguration.isStarttlsEnable());
        account.setSslEnable(emailConfiguration.isSslEnable());
        account.setAuth(true);

        String send = Mail.create(account)
                .setTos(email)
                .setTitle(title)
                .setContent(s)
                .setHtml(true)
                .send();

        log.info(send);
    }

    @SneakyThrows
    public String uploadFile(String bucketName, MultipartFile file) {
        String fileName = IdUtil.simpleUUID() + StrPool.DOT + FileUtil.extName(file.getOriginalFilename().toLowerCase());

        String pathPattern = "/%s/%s/%s/%s"; // /bucketName/year/month/文件名.后缀名

        LocalDate now = LocalDate.now();
        String path = String.format(pathPattern, bucketName, now.getYear(), now.getMonthValue(), fileName);

        ApplicationHome h = new ApplicationHome(getClass());
        File source = h.getSource();

        @Cleanup InputStream inputStream = file.getInputStream();

        FileUtil.writeFromStream(inputStream, new File(source, "/file" + path));
        return path;
    }

}
